package com.linked.basemanage.file.service.impl;

import com.linked.basemanage.file.bean.po.FileInfoPO;
import com.linked.basemanage.file.mapper.IFileInfoMapper;
import com.linked.basemanage.file.service.IFileHandleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@Slf4j
public class FileHandleServiceImpl implements IFileHandleService {


    private IFileInfoMapper fileInfoMapper;

    public FileHandleServiceImpl(IFileInfoMapper fileInfoMapper) {
        this.fileInfoMapper = fileInfoMapper;
    }

    @Override
    public int setFilesBelong(List<String> fileIds, String belongId, String belongTable) throws Exception {
        AtomicInteger updateCount = new AtomicInteger(0);
        fileIds.stream().forEach(fileId -> {
            FileInfoPO fileInfo = new FileInfoPO();
            fileInfo.setFileId(fileId);
            fileInfo.setBelongId(belongId);
            fileInfo.setBelongTable(belongTable);
            updateCount.addAndGet(fileInfoMapper.updateById(fileInfo));
        });
        return updateCount.get();
    }

    @Override
    public boolean saveFileInfo(FileInfoPO fileInfo) throws Exception {
        return fileInfoMapper.insert(fileInfo) > 0;
    }
}
